package ru.spb.osll.myfit.server.handler;

import org.apache.log4j.Logger;
import ru.spb.osll.myfit.common.Include;
import ru.spb.osll.myfit.common.Page;
import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.server.Configurator;
import ru.spb.osll.myfit.server.Converter;
import ru.spb.osll.myfit.server.parser.PageParser;
import ru.spb.osll.myfit.server.parser.PageUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Vassar
 * Date: 23.05.2011
 * Time: 19:49:41
 * To change this template use File | Settings | File Templates.
 */
public class ParentVariablesCreator {
  private static Logger LOG = Logger.getLogger(PageLoader.class);
  private String m_root;
  private String CONTENT = "content.txt";
  private PageLoader pageLoader;
  private PageParser m_parser;

  public ParentVariablesCreator() {
    m_root = Configurator.getInstance().getRoot();
    pageLoader = new PageLoader();
    m_parser = new PageParser(new Converter());
  }



  public HashMap<String, String> getParentVariablesMap(String fileName) {
    HashMap<String, String> parentVariables = new HashMap<String,String>();
    List<String> parentList = new ArrayList<String>();
    String path = fileName.substring(0, fileName.indexOf(m_root) + m_root.length());

    if (!fileName.equals(path + File.separator + CONTENT)) {
      parentList.add(path + File.separator + CONTENT);
      getListPath(parentList, path, fileName);
      parentVariables = getParentVariables(parentList);
    }


    return parentVariables;
  }

  private void getListPath(List<String> list, String path, String fileName) {
    String str = fileName.substring(path.length()+1);
    str = str.substring(0, str.indexOf(File.separatorChar));
    if (!fileName.equals(path + File.separator + str + File.separator + CONTENT)) {
      list.add(path + File.separator + str + File.separator + CONTENT);
      getListPath(list, path + File.separator + str, fileName);
    }
  }

  private HashMap<String, String> getParentVariables(List<String> list) {
    HashMap<String, String> map = new HashMap<String, String> ();
    for(String path : list) {
      try {
        Page page = loadPageWithoutParentVariables(path);
        if (page.getVariables() != null && page.getVariables().size() != 0)
            map.putAll(page.getVariables());
      } catch (IOException e) {
              LOG.error("File \"" + path + "\" does not exist!");
      }
    }

      return map;
  }

  public Page loadPageWithoutParentVariables(String fileName)
          throws IOException{
    Page page = pageLoader.loadPage(fileName);
    for (PageElement element : page.getElements()) {
      if (element instanceof Include) {
        String includePath = ((Include) element).getPath();
        Page includePage = loadPageWithoutParentVariables(PageUtils
          .getFullPath(m_root, includePath));
        if (includePage.getVariables() != null &&
            includePage.getVariables().size() != 0) {
          page.addVariables(includePage.getVariables());
        }
      }
    }

    return page;
  }
}
